package com.wanxi;

import java.util.ArrayList;
import java.util.Scanner;

public class ManagementSystem {
    public static void main(String[] args) throws InterruptedException {
        ArrayList<Student> allStudent = new ArrayList<Student>();
        Scanner scanner = new Scanner(System.in);
        ManagementSystem managementSystem = new ManagementSystem();
        int sid = 0;
        while (true) {
            int selection = managementSystem.getSelection(scanner);//得到操作选择
            switch (selection) {
                case 1://添加学生
                    sid += 1;
                    managementSystem.addStudent(allStudent, scanner, sid);
                    break;
                case 2://删除学生
                    managementSystem.deleteStudent(allStudent, scanner);
                    break;
                case 3://修改学生
                    managementSystem.changeStudent(allStudent, scanner);
                    break;
                case 4://通过Id查找学生
                    managementSystem.findStudent(allStudent, scanner);
                    break;
                case 5://查看所有学生信息
                    managementSystem.showStudent(allStudent);
                    break;
                case 6://退出操作系统
                    managementSystem.exitSystem();
                    break;
                default:
                    System.out.println("你的输入操作有误，请重新输入！");
                    break;
            }
        }
    }

//    /**
//     * 根据输入的ID查找学生对象，并返回一个学生对象。暂未使用，，，，
//     *
//     * @param allStudent
//     * @param id
//     * @return
//     */
//    private Student getStudent(ArrayList<Student> allStudent, int id) {
//        Student student = null;
//        for (int i = 0; i < allStudent.size(); i++) {
//            Student findStudent = allStudent.get(i);
//            if (id == student.getSid()) {
//                student = findStudent;
//            }
//        }
//        return student;
//    }


    /**
     * 退出系统操作
     *
     * @throws InterruptedException
     */
    private void exitSystem() throws InterruptedException {
        for (int i = 3; i >= 0; i--) {
            Thread.sleep(500);
            System.out.print(i + " ");
        }
        System.out.println("已退出管理系统");
        System.exit(0);
    }

    /**
     * 查看所有学生信息
     *
     * @param allStudent
     */
    private void showStudent(ArrayList<Student> allStudent) {
        for (int i = 0; i < allStudent.size(); i++) {
            Student theStudent = allStudent.get(i);
            System.out.println(theStudent);
        }
    }

    /**
     * 通过Id查找学生
     *
     * @param allStudent
     * @param scanner
     */
    private void findStudent(ArrayList<Student> allStudent, Scanner scanner) {
        System.out.println("请输入需要查找的学生ID： ");
        int findSid = scanner.nextInt();
        int findTime = 0;
        for (int i = 0; i < allStudent.size(); i++) {
            Student searchStudent = allStudent.get(i);
            if (findSid == searchStudent.getSid()) {
                findTime++;
                System.out.println(searchStudent);
            }
        }
        if (findTime == 0) {
            System.out.println("未找到该学生ID的学生信息");
        }
    }

    /**
     * 通过Id查找并修改学生信息。
     *
     * @param allStudent
     * @param scanner
     */
    private void changeStudent(ArrayList<Student> allStudent, Scanner scanner) {
        System.out.println("请输入需要修改的学生ID： ");
        int changeSid = scanner.nextInt();
        System.out.println("请输入需要修改的学生新信息：姓名，年龄，出生年月");
        String changeName = scanner.next();
        int changeAge = scanner.nextInt();
        String changeBirthday = scanner.next();
        int changeTime = 0;
        for (int i = 0; i < allStudent.size(); i++) {
            Student changeStudent = allStudent.get(i);
            if (changeSid == changeStudent.getSid()) {
                changeTime++;
                changeStudent.setName(changeName);
                changeStudent.setAge(changeAge);
                changeStudent.setBirthday(changeBirthday);
                System.out.println("学生信息修改成功");
            }
        }
        if (changeTime == 0) {
            System.out.println("未找到该学生ID的学生信息");
        }
    }

    /**
     * 通过Id查找并删除学生的操作
     *
     * @param allStudent
     * @param scanner
     */
    private void deleteStudent(ArrayList<Student> allStudent, Scanner scanner) {
        System.out.println("请输入需要删除的学生ID： ");
        int deleteSid = scanner.nextInt();
        int deleteTime = 0;
        for (int i = 0; i < allStudent.size(); i++) {
            Student deleteStudent = allStudent.get(i);
            if (deleteSid == deleteStudent.getSid()) {
                deleteTime++;
                allStudent.remove(i);
                System.out.println("已删除该学生ID的学生信息");
            }
        }
        if (deleteTime == 0) {
            System.out.println("未找到该学生ID的学生信息");
        }
    }

    /**
     * 添加学生的操作
     *
     * @param allStudent
     * @param scanner
     * @param sid
     */
    private void addStudent(ArrayList<Student> allStudent, Scanner scanner, int sid) {
        System.out.println("请输入需要添加的学生姓名，年龄，出生年月： ");
        String name = scanner.next();
        int age = scanner.nextInt();
        String birthday = scanner.next();
        Student addStudent = new Student(sid, name, age, birthday);
        allStudent.add(addStudent);
        System.out.println("添加学生成功，如此查看，可点击选项5：查看所有学生");
    }

    /**
     * 返回操作选择
     *
     * @param scanner
     * @return
     */
    private int getSelection(Scanner scanner) {
        System.out.println("---------欢迎来到学生管理系统---------");
        System.out.println("1 添加学生");
        System.out.println("2 删除学生");
        System.out.println("3 修改学生");
        System.out.println("4 查找学生");
        System.out.println("5 查看所有学生");
        System.out.println("6 退出系统");
        System.out.println("请输入你的选择：");
        int selection = scanner.nextInt();
        return selection;
    }
}
